local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local UIManager = ClientOHOH.load.UIManager();
local LanguageHelper= _G.LanguageHelper
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local CarTool=_G.CommonOHOH.load.CarTool();
local recipe_cfg=_G.CommonOHOH.config.Recipe
local function_list =_G.ClientOHOH.function_list

ClientOHOH.load.RecipeCell()

local instStr={
   scroll="ctrl.relic.navScrolling",

   navFrame = "ctrl.relic.navFrame",
   box = "ctrl.relic.box",

   cellBox="ctrl.relic.btn.cellBox",
   lockIcon="ctrl.relic.btn.lockImg",
   
   name="ctrl.relic.objName",
   describe="ctrl.relic.objDesc",

   lockText = "ctrl.relic.btn.lockText",

   makeBtn = "ctrl.relic.btn.make",
   price = "ctrl.relic.btn.price",
   equipBtn = "ctrl.relic.btn.equip",
   unEquipBtn = "ctrl.relic.btn.unEquip",
}

function M.new(...)
	return M(...);
end;

function M.ctor(self,ctrl)
   M.super.ctor(self);
   self.ctrl=ctrl
   self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)
   self.RelicList = {}

   CallBackManager:AddRbxConnect(self,self.ui.makeBtn.Activated, function()
      self:HideTip()
      local make = self:RecipeMake( self.SelectedNavCell:GetRecipeCfgData())
      if make[1] then
         self.ui.makeBtn.Visible = false
         self.ui.equipBtn.Visible = true
      end 
	end);

   CallBackManager:AddRbxConnect(self,self.ui.equipBtn.Activated, function()
      self:HideTip()
		self:Equip(self.SelectedNavCell:GetRecipeCfgData())
	end);

   CallBackManager:AddRbxConnect(self,self.ui.unEquipBtn.Activated, function()
      self:HideTip()
		self:UnEquip(self.SelectedNavCell:GetRecipeCfgData())
	end);
   
end;

--遗物切换
function M.NavChange(self,cell)
   if self.popInfoPartCell then
      self.popInfoPartCell:HideTip()
   end
	if self.SelectedNavCell == nil then
		self.SelectedNavCell = cell
		cell:SetSelectedStatus(true)	
		return
	end

	if self.SelectedNavCell == cell then
		-- self.SelectedNavCell = nil
		-- cell:SetSelectedStatus(false)
	else
		self.SelectedNavCell:SetSelectedStatus(false)
		cell:SetSelectedStatus(true)
		self.SelectedNavCell = cell
	end

end

--合成物品查看切换
function M.PartChange(self,cell)
	if self.SelectedPartCell == nil then
		self.SelectedPartCell = cell
      cell:ShowTip()
      self.popInfoPartCell =cell
		return
	end

	if self.SelectedPartCell == cell then
		self.SelectedPartCell = nil
      cell:HideTip()
      self.popInfoPartCell = nil

	else

      cell:ShowTip()
      self.popInfoPartCell =cell
		self.SelectedPartCell = cell
	end
end

--创建部件Cell
function M.CreatePartCell(self,carId,hadnum,neednum,islock)
   local cell=  require(script.Parent.Cell).new(carId,self.ui.cellBox,nil,nil,hadnum)
   cell:SetTitleVisibility(false)
   cell:SetPartInVisibility(false)
   cell:SetPartWeightVisibility(false)
   cell:SetPartNumVisibility(false)
   if  islock then
      cell:SetCellLockLook(self.ui.lockIcon)
   else
      cell:SetPartInVisibility(true)
      cell:SetPartWeightVisibility(true)
      cell:SetPartNum(hadnum,neednum)
      cell:SetPartNumVisibility(true)
      cell.ClickEvent:Connect(function()
         self:PartChange(cell)
      end)
   end
end

--创建配方cell
function M.CreateRecipeCell(self,recipeId)
   local cell = ClientOHOH.load.RecipeCell().new(recipeId,self.ui.scroll,self.ctrl:GetDataBase())
   -- cell.Lock = true
   cell.ClickEvent:Connect(function()
      self:CellClick(cell)
   end)
   return cell
end

--更新遗物的信息（例如名字 buff）
function M.UpdataInfo(self,cell)
   local lockStatus = cell:GetLockStatus()
   --清空列表
   for v2, v3 in ipairs(self.ui.cellBox:GetChildren()) do
      if v3.ClassName == "Frame" then
         v3:Destroy()
      end;
   end;
   --重置UI
   self.ui.lockText.Visible = true
   self.ui.makeBtn.Visible = false
   self.ui.price.Text = math.huge
   self.ui.equipBtn.Visible = false
   self.ui.unEquipBtn.Visible = false

   local data = cell:GetRecipeCfgData()

   --遗物名称显示
   self.ui.name.Text = LanguageHelper:GetText(data.name)
  
   --遗物属性显示
   if lockStatus then
      self.ui.describe.Text = "???"
   else
      local result = {}
      for i = 1, #data.property do
         local prop = LanguageHelper:GetText(5554000+data.property[i])   -- 如果没有对应的值，使用"N/A"
         local val = data.value[i] 
         local valType = data.valueType[i]
         local valString = nil
         if valType == 1 then
            valString = BigNumberHelper:ToString(val)
         else
            valString = BigNumberHelper:ToString(val*100).."%"
         end
         
         table.insert(result, string.format("+%s %s", valString, prop))
      end
     local txtString =  table.concat(result, " , ")
     self.ui.describe.Text = txtString
   end
   

  --合成Part显示
   for k,value in pairs(data.itemData) do
      local carName, carId = k:match("^(%w+)_(%d+_%d+)$")
      local hadNum = self.HadData[carId]
      if not hadNum then
         hadNum = 0
      end
      self:CreatePartCell(carId,hadNum,value,lockStatus)
   end
   if  lockStatus then
      self.ui.lockText.Visible = true
      return
   else
      self.ui.lockText.Visible = false
   end
   --按钮显示

   local costTable = cell:GetRecipeCost()
   local costNum 
   for k,v in pairs(costTable) do
      costNum = v
   end

   if cell:GetIsMake() then
      if cell:GetIsEquip() then
         self.ui.unEquipBtn.Visible = true
         self.ui.equipBtn.Visible = false
      else
         self.ui.equipBtn.Visible = true
         self.ui.unEquipBtn.Visible = false
      end
   else
      self.ui.price.Text = costNum
      self.ui.makeBtn.Visible = true
      self.ui.unEquipBtn.Visible = false
      self.ui.equipBtn.Visible = false
   end
   
end

--遗物Cell点击
function M.CellClick(self,cell)
   self:NavChange(cell)
   self:UpdataInfo(cell)
end

--初始化
function M.InitBox(self)
      -- 创建一个新的表用于存储过滤后的数据
      local filtered_recipe_cfg = {}

      -- 过滤出 makeType 等于 1 的数据
      for key, value in pairs(recipe_cfg) do
         if value.type == 1 then
               filtered_recipe_cfg[key] = value
         end
      end
      -- 创建一个按顺序存储 key 的数组
      local keys = {}
      for key, _ in pairs(filtered_recipe_cfg) do
         table.insert(keys, key)
      end
      -- 按 orderId 从大到小排序
      table.sort(keys, function(a, b)
         return filtered_recipe_cfg[a].orderId > filtered_recipe_cfg[b].orderId
      end)
      -- 输出排序结果
      for _, key in ipairs(keys) do
         local id  = filtered_recipe_cfg[key].id
         if next(self.RelicList)== nil then
            self.RelicList[id] =self:CreateRecipeCell(id)
            self.RelicList[id]:SetIsEquip(false)
            self.RelicList[id]:SetIsMake(false)
            self:CellClick(self.RelicList[id])
         end
         if  self.RelicList[id] == nil then
            self.RelicList[id] =self:CreateRecipeCell(id)
         end 
      end
end

--更新左侧遗物列表
function M.UpdateNav(self,recipeData,isInit)
   if next(recipeData.relic) == nil then
      return
   end
   for _,k in ipairs(recipeData.relic) do
      if self.RelicList[k] then
         self.RelicList[k]:SetLockStatus(false)
         self.RelicList[k]:SetIsMake(false)
         self.RelicList[k]:SetIsEquip(false)
      end
   end
   for _,k in ipairs(recipeData.make) do
      if self.RelicList[k] then
         self.RelicList[k]:SetIsMake(true)
      end
   end
   for _,k in ipairs(recipeData.equip) do
      if self.RelicList[k] then
         self.RelicList[k]:SetIsEquip(true)
         -- if isInit then
         --    print("diyici")
         --    self:Equip(self.RelicList[k]:GetRecipeCfgData())
         -- end
        
      end
   end
end

--更新整个Relic
function M.UpdateBox(self,recipeData,carData)
   if carData then
      self.HadData = carData.had
      if self.SelectedNavCell then
         self:UpdataInfo(self.SelectedNavCell)
      end
   end
   --List为空则重新初始化
   if next(self.RelicList)== nil then
      self:InitBox()
      self:UpdateNav(recipeData,true)
      if self.SelectedNavCell then
         self:UpdataInfo(self.SelectedNavCell)
      end
      return
  end
  if recipeData then
      self:UpdateNav(recipeData)
      if self.SelectedNavCell then
         self:UpdataInfo(self.SelectedNavCell)
      end
   end
end

function M.RecipeMake(self,rCfg)
   --同步数据
   return self.ctrl.RelicMakeEvent:Action(rCfg)--recipeID
end
function M.Equip(self,rCfg)
   --同步数据
   self.ctrl.RelicEquipEvent:Action(rCfg)
end
function M.UnEquip(self,rCfg)
   --同步数据
   self.ctrl.RelicUnEquipEvent:Action(rCfg)
end
function M.HideTip(self)
   if self.popInfoPartCell then
      self.popInfoPartCell:HideTip()
   end
end

function M.dtor(self)
	M.super.dtor(self);
end;

---
function M.Hide(self)
   self.ui.navFrame.Visible = false 
   self.ui.box.Visible = false 
end
function M.Show(self)
   self.ui.navFrame.Visible = true
   self.ui.box.Visible = true 
end


return M